package com.globalwave.common.service;

import com.globalwave.base.BaseServiceImpl;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.entity.Catalog;
import com.globalwave.common.entity.CatalogCriterion;

public class CatalogServiceImpl 
       extends BaseServiceImpl 
       implements CatalogService {

	protected Class<?> catalogType ;
	
    public Catalog create(Catalog catalog) throws Exception {
        return (Catalog) jdbcDao.insert(catalog);
    }

    public void update(Catalog catalog) throws Exception {
        jdbcDao.update(catalog) ;
    }

    public void delete(Catalog catalog) throws Exception {
        // delete cascade 
/*        OrganizationCatalog orgCatalog = new OrganizationCatalog() ;
        orgCatalog.setCatalog_id(catalog.getCatalog_id()) ;
        jdbcDao.delete(OrganizationCatalog.class, orgCatalog) ;*/
        
        jdbcDao.delete(catalog) ;
        
    }

    public void deleteAll(Long[] catalogIds) throws Exception {
        CatalogCriterion criterion = new CatalogCriterion() ;
        criterion.setCatalogIds(catalogIds) ;

        if (catalogType == null) {
            jdbcDao.delete(Catalog.class, criterion) ;
        } else {
        	jdbcDao.delete(catalogType, criterion) ;
        }
        
    }

    public ArrayPageList<Catalog> query(
            CatalogCriterion catalogCriterion, 
            int pageIndex, 
            int pageSize, 
            ArrayOrderList orderList) throws Exception {
        
        if (orderList == null) {
            orderList = new ArrayOrderList() ;
            orderList.addAsc("level_") ;
            orderList.addAsc("index_") ;
            orderList.addAsc("catalog_id") ;
        }
        
        if (catalogType == null) {
            return (ArrayPageList<Catalog>)jdbcDao.query(catalogCriterion, pageIndex, pageSize, orderList, Catalog.class);
        } else {
        	return (ArrayPageList<Catalog>)jdbcDao.query(catalogCriterion, pageIndex, pageSize, orderList, catalogType);
        }
    }


    /* query use sql
    public ArrayPageList<Catalog> foo(Long id) throws Exception {
        return jdbcDao.query(Sql, Catalog.class, null, null);
    }*/
    
}
